package com.evon.yardmanagement.export;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * Class responsible for exporting the data into excel.
 * 
 * @author vgaurav
 * 
 */
public class ExportDataManager<T extends ExportableData> {

	// Wrapping the export able data which has to be exported into excel.
	T exportableData;

	public ExportDataManager(T exportData) {
		this.exportableData = exportData;
	}

	/**
	 * This method takes the data from @exportableData and write that content
	 * into the spread sheet.
	 */
	public ByteArrayOutputStream exportToExcel() {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			WritableWorkbook workbook = Workbook.createWorkbook(bos);

			WritableSheet sheet = workbook.createSheet(exportableData
					.getDataName(), 0);

			writeHeader(sheet);
			writeData(sheet);

			workbook.write();
			workbook.close();
		} catch (RowsExceededException e) {
			e.printStackTrace();
			// TODO : throw exception
		} catch (WriteException e) {
			e.printStackTrace();
			// TODO : throw exception
		} catch (IOException e) {
			e.printStackTrace();
			// TODO : throw exception
		}
		return bos;
	}

	/**
	 * Write the export data into the sheet.
	 * 
	 * @param s
	 * @throws WriteException
	 * @throws RowsExceededException
	 */
	private void writeData(WritableSheet s) throws WriteException,
			RowsExceededException {
		WritableFont wfdata = new WritableFont(WritableFont.ARIAL, 10);
		WritableCellFormat cfdata = new WritableCellFormat(wfdata);

		for (int rowCounter = 0; rowCounter < exportableData.getRowCount(); rowCounter++) {
			for (int columnCounter = 0; columnCounter < exportableData
					.getColumnCount(); columnCounter++) {
				Label cellValue = new Label( columnCounter,rowCounter+1 ,
						exportableData.getValueAt(rowCounter, columnCounter),
						cfdata);
				s.addCell(cellValue);
			}
		}
	}

	/**
	 * Write the header data into the sheet.
	 * 
	 * @param s
	 * @throws WriteException
	 * @throws RowsExceededException
	 */

	private void writeHeader(WritableSheet s) throws WriteException,
			RowsExceededException {
		/* Format the Font */
		WritableFont wfheader = new WritableFont(WritableFont.ARIAL, 12,
				WritableFont.BOLD);
		WritableCellFormat cfheader = new WritableCellFormat(wfheader);

		// cfheader.setWrap(true);

		/* Creates Label and writes date to one cell of sheet */

		for (int counter = 0; counter < exportableData.getColumnCount(); counter++) {
			// second param is 0 as this is header tag.
			Label header = new Label(counter, 0, exportableData
					.getColumnHeader().get(counter), cfheader);
			s.addCell(header);
		}
	}

}
